package cn.edu.thu.tsqualitylite.controller.fanDataProblem;

import cn.edu.thu.tsquality.core.common.datasource.CsvDataSource;
import cn.edu.thu.tsquality.core.common.datasource.IDataSource;
import cn.edu.thu.tsquality.core.common.util.ArgumentsHelper;
import cn.edu.thu.tsqualitylite.common.constant.Algorithms;
import cn.edu.thu.tsqualitylite.common.constant.Constants;
import cn.edu.thu.tsqualitylite.common.util.ServerTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class InconsistentCRGraphController
{

    @RequestMapping(value = "/api/fan-data-problem/inconsistent-CR-graph", method = RequestMethod.POST)
    @ResponseBody
    public List<ServerTable> runMultiputAlgorithm(@RequestBody Map<String, Object> params
    ) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {


        String algorithmName = "fan-data-problem-cr";

        String inputFilenames = params.get("inputFilenames").toString();
        String outputFilenames = params.get("outputFilenames").toString();

        String parameters = "K";

        String[] inputFileArray = ArgumentsHelper.getValues(inputFilenames);
        for (int i = 0; i < inputFileArray.length; ++i) {
            if (!inputFileArray[i].startsWith("/") && inputFileArray[i].charAt(1) != ':') {
                inputFileArray[i] = Constants.DATA_PATH + inputFileArray[i];
            }
        }
        inputFilenames = ArgumentsHelper.buildValues(inputFileArray);
        String[] outputFileArray = ArgumentsHelper.getValues(outputFilenames);
        for (int i = 0; i < outputFileArray.length; ++i) {
            if (!outputFileArray[i].startsWith("/") && outputFileArray[i].charAt(1) != ':') {
                outputFileArray[i] = Constants.FAN_REPAIR_PATH + outputFileArray[i];
            }
        }
        outputFilenames = ArgumentsHelper.buildValues(outputFileArray);

        Class algorithm = Algorithms.getAlgorithm(algorithmName);

        Method method = algorithm.getMethod("main", String[].class);
        String[] args = new String[]{inputFilenames, outputFilenames, parameters};

        method.invoke(null, (Object) args);

        List<ServerTable> results = new ArrayList<>();
        int cnt=0;
        for (String outputFilename : outputFileArray) {
            cnt ++;
            if (cnt==1) {
                try {
                    IDataSource dataSource = new CsvDataSource(outputFilename);
                    results.add(new ServerTable(dataSource.read()));
                } catch (IOException e) {
                    results.add(null);
                }
            }
        }
        return results;
    }
}

